package com.sky.Protocol;

/**
 * Created by sun on 2017-06-11.
 */

import com.sky.Util.BytesUtil;
import io.netty.buffer.ByteBuf;

/**
 * 协议包头
 */
public class MessageHeader {

    /**
     * 协议版本
     */
    private int version;

    /**
     * 消息类型
     */
    private int msgType;

    /**
     * 标签 0x0 不加密 0x1 加密
     */
    private int tag;

    /**
     * 包序号，客户端连续发包需要把包序号每次递增
     */
    private int packId;

    public static final int HEADER_LENGTH = 4 * 4;

    public MessageHeader() {

    }

    public MessageHeader(int version,int msgType,int tag,int packId) {
        this.version = version;
        this.msgType = msgType;
        this.tag = tag;
        this.packId = packId;
    }

    public MessageHeader(byte[] bytes) {

        this.version = BytesUtil.bytes2Int(bytes,0,4);
        this.msgType = BytesUtil.bytes2Int(bytes,4,4);
        this.tag = BytesUtil.bytes2Int(bytes,8,4);
        this.packId = BytesUtil.bytes2Int(bytes,12,4);
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public int getMsgType() {
        return msgType;
    }

    public void setMsgType(int msgType) {
        this.msgType = msgType;
    }

    public int getTag() {
        return tag;
    }

    public void setTag(int tag) {
        this.tag = tag;
    }

    public int getPackId() {
        return packId;
    }

    public void setPackId(int packId) {
        this.packId = packId;
    }

    /**
     * 序列化网络字节序
     */
    public void msgHeader2ByteBuf(ByteBuf byteBuf) {
        byteBuf.writeInt(version);
        byteBuf.writeInt(msgType);
        byteBuf.writeInt(tag);
        byteBuf.writeInt(packId);
    }
}
